//
//  UnderInherite.m
//  smarket
//
//  Created by client on 2017/9/25.
//  Copyright © 2017年 原研三品. All rights reserved.
//

#import "UnderInherite.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "MethodSignatureObject.h"

void (*objc_msgSend_func)(id self,SEL selector,id obj1 ,id obj2,id obj3) = (void (*) (id,SEL,id,id,id))objc_msgSend;

@interface UnderInherite ()
{
    NSMethodSignature * _methodSignature;
}

@end

@interface UnderInherite ()

@end

@implementation UnderInherite

void UnderlyingInheriteMethod(id self,SEL selector)
{
    if ([self isKindOfClass:[UnderInherite class]]) {
    }
}

void UnderReplaceQueueMethod(id self,SEL selector)
{
    if ([self isKindOfClass:[UnderInherite class]]) {
        objc_msgSend_func(self,NSSelectorFromString(@"testClass:obj:obj2:"),@"obj1",@"obj2",@"obj3");
    }
}

-(void)testClass:(NSString*)classIndefiner obj:(NSString*)obj1 obj2:(NSString*)obj2
{
}

-(void)testName:(NSString*)name;
{
    
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        unsigned int count = 0;
        objc_property_t * propertys = class_copyPropertyList([UnderInherite class], &count);
        for (int i =0; i < count; i++) {
            objc_property_t property = propertys[i];
            const char * propertyName = property_getName(property);
            NSString * key = [NSString stringWithUTF8String:propertyName];
            id  value=  [coder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    unsigned int count = 0;
    objc_property_t * propertys = class_copyPropertyList([UnderInherite class], &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertys[i];
        const char * propertyName = property_getName(property);
        NSString * key = [NSString stringWithUTF8String:propertyName];
        NSString * value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
}

+(void)load
{
    class_addMethod([self class], @selector(underlyingQueue:), (IMP)UnderlyingInheriteMethod, "v@:");
    class_replaceMethod([self class], @selector(underReplaceQueue:selector:), (IMP)UnderReplaceQueueMethod, "v@:");
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self fetchAllMethods];
    }
    return self;
}

-(NSObject*)objects
{
    return [NSObject new];
}

-(void)fetchAllMethods
{
    unsigned int count = 0;
    Method * methodlists = class_copyMethodList([self class], &count);
    for (unsigned int i = 0; i < count; i++) {
        Method  method = methodlists[i];
        SEL methodName = method_getName(method);
        NSLog(@"方法名:%@",NSStringFromSelector(methodName));
        //获取方法的参数类型
        unsigned int argumentCount = method_getNumberOfArguments(method);
        char argName[1024] = {};
        for (unsigned int j = 0 ; j < argumentCount;j++) {
            method_getArgumentType(method, j, argName, 1024);
            NSLog(@"第%u个参数类型：%s",j,argName);
        }
        char returnType[512] ={};
        method_getReturnType(method, returnType, 512);
        NSLog(@"返回值类型:%s",returnType);
        //type enCoding
        NSLog(@"typeEncoding：%s", method_getTypeEncoding(method));
    }
}

//消息转发机制
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
    return YES;
}

- (void)doesNotRecognizeSelector:(SEL)aSelector;
{
   
}

-(id)forwardingTargetForSelector:(SEL)aSelector
{
    if ([[self class]instancesRespondToSelector:aSelector]) {
        return self;
    }
    return nil;
}

-(void)forwardInvocation:(NSInvocation *)anInvocation
{
//    [anInvocation invokeWithTarget:self.signatureObject];
}

-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
//   _methodSignature = [self.signatureObject methodSignatureForSelector:aSelector];
    return nil;
}

@end
